var SCREEN_MENU = 'screen_menu';
var SCREEN_SECTION = 'screen_section';
var SCREEN_DETAIL = 'screen_detail';

var currentScreen = null;
var screenContainer = null;
var screenHistory = new Array();

var currentSectionIndex = -1;
var sectionItems = null;

var sections = new Array(
	"Nokia Developer News",
	"Forum Nokia Web Developer Alert",
	"All About Symbian"
);
var section_urls = new Array(
	"http://blogs.forum.nokia.com/rss.php?blogId=300129&profile=rss20",
	"http://blogs.forum.nokia.com/rss.php?blogId=300119&profile=rss20",
	"http://www.allaboutsymbian.com/rss/news/rss2fall.xml"
);

function $(id)
{
	return document.getElementById(id);
}

function init()
{
	screenContainer = $('screen_container');
	
	setupHomeScreen();
}
function setupHomeScreen()
{
	var screen = createScreen(SCREEN_MENU);
	
	for(var i = 0; i < sections.length; i++)
	{
		var el = document.createElement('div');

		el.innerHTML = sections[i];
		
		el.itemIndex = i;
		
		el.onclick = sectionClicked;
		
		el.className = 'item';
		
		screen.appendChild(el);
	}
	
	gotoScreen(screen);
}
function gotoSection(sectionIndex)
{
	currentSectionIndex = sectionIndex;
	
	var screen = createScreen(SCREEN_SECTION);
	
	screen.innerHTML = '<img class="loader" src="images/ajax-loader.gif" />';
	
	gotoScreen(screen);
	
	loadXML(section_urls[sectionIndex], sectionDataHandler)
}
function sectionDataHandler(xml)
{
	if(xml)
	{
		var items = xml.getElementsByTagName('item');
		
		sectionItems = new Array(items.length);
		
		var itemChildren, childName;
		
		for(var i = 0; i < items.length; i++)
		{
			sectionItems[i] = new Object();
			
			itemChildren = items.item(i).childNodes;
			
			for(var j = 0; j < itemChildren.length; j++)
			{
				childName = itemChildren.item(j).nodeName;
				
				if(childName == 'title')
					sectionItems[i].title = getNodeText(itemChildren.item(j))
				else if(childName == 'description')
					sectionItems[i].description = getNodeText(itemChildren.item(j))
				else if(childName == 'enclosure')
				{
					sectionItems[i].image = itemChildren.item(j).attributes.getNamedItem('url').nodeValue;
				}
			}
		}
		showSectionItems();
	}
	else
	{
		showError("Impossibile recuperare i dati della sezione");
	}
}
function exit()
{
	window.close();
}

function showSectionItems()
{
	if(currentScreen.id == SCREEN_SECTION)
	{
		currentScreen.innerHTML = '';
		
		var title = document.createElement('div');
		title.className = 'screen_title';
		title.innerHTML = sections[currentSectionIndex];
		currentScreen.appendChild(title);
		
		for(var i = 0; i < sectionItems.length; i++)
		{
			var el = document.createElement('div');
	
			el.innerHTML = sectionItems[i].title;
			
			el.itemIndex = i;
			
			el.onclick = sectionItemClicked;
			
			el.className = 'item';
			
			currentScreen.appendChild(el);
		}
	}
}
function gotoItemDetail(itemIndex)
{
	var screen = createScreen(SCREEN_DETAIL);
	
	var item = sectionItems[itemIndex];
	
	/* element title */
	var el = document.createElement('div');
	el.className = 'screen_title';
	el.innerHTML = item.title;
	screen.appendChild(el);
	
	/* item image */
	if(item.image != undefined)
	{
		var img = document.createElement('img');
		img.src = item.image;
		screen.appendChild(img);
	}
	
	/* item description */
	var desc = document.createElement('div');
	desc.className = 'screen_text';
	desc.innerHTML = item.description;
	screen.appendChild(desc);
	
	gotoScreen(screen);
}
function sectionItemClicked()
{
	gotoItemDetail(this.itemIndex);
}
function sectionClicked()
{
	gotoSection(this.itemIndex);
}
function gotoScreen(screen, goBack)
{
	widget.prepareForTransition('fade');
	
	screenContainer.appendChild(screen);
	
	if(currentScreen != null)
	{
		if(!goBack)
			screenHistory.push(currentScreen);
		
		currentScreen.parentNode.removeChild(currentScreen);
	}
	currentScreen = screen;
	
	widget.performTransition();

	if(screenHistory.length > 0)
	{
		menu.setRightSoftkeyLabel('Back', gotoPreviousScreen);
	}
	else
	{
		menu.setRightSoftkeyLabel('Exit', exit);
	}
}
function gotoPreviousScreen()
{
	if(screenHistory.length > 0)
	{
		var prevScreen = screenHistory.pop();
		
		gotoScreen(prevScreen, true);
	}
}
function createScreen(screenId)
{
	var screen = document.createElement('div');
	
	screen.id = screenId;
	
	screen.className = 'screen';
	
	return screen;
}


/* utils */

function getNodeAttribute(node, attr)
{
	return node.attributes.getNamedItem(attr).nodeValue;
}
function getNodeText(node)
{
	return node.firstChild.nodeValue;
}
function showError(txt)
{
	alert(txt);
}

function loadXML(xmlFile, func)
{
//	netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
	
	try
	{
		var request = new XMLHttpRequest();
		request.open( "GET", xmlFile, true );
		request.send(null);

		request.onreadystatechange = function(){
			if( this.readyState == 4)
			{
				if(this.status == 200)
				{
					func(request.responseXML);
				}
				else
				{
					func(false);
				}
			}
		}
	}
	catch( e )
	{
		alert(e);
		
		func(false);
	}
}